CC=i586-mingw32msvc-gcc
CFLAGS=-O2 -s -Wall -D_WIN32_WINNT=0x0500 -Icommon -Idll -Iext/include -Lext -DUSE_UPNP
WINDRES=i586-mingw32msvc-windres
LIBS=ext/libminiupnpc.a -lws2_32 -liphlpapi
REV=$(shell sh -c 'svnversion .')

all: debug

resources/resource.o: resources/launcher.rc
	$(WINDRES) -J rc resources/launcher.rc resources/resource.o

debug: resources/resource.o launcher/launcher.c launcher/hook_thipx32.c launcher/hook_wsock32.c dll/thipx32.c dll/wsock32.c common/sockets.c common/config.c common/upnp.c
	$(CC) $(CFLAGS) -D_DEBUG -o launcher.exe launcher/main.c launcher/launcher.c launcher/hook_thipx32.c launcher/hook_wsock32.c dll/thipx32.c dll/wsock32.c common/sockets.c common/config.c common/upnp.c resources/resource.o $(LIBS)

nodebug: resources/resource.o launcher/launcher.c launcher/hook_thipx32.c launcher/hook_wsock32.c dll/thipx32.c dll/wsock32.c common/sockets.c common/config.c common/upnp.c
	$(CC) $(CFLAGS) -mwindows -o launcher.exe launcher/main.c launcher/launcher.c launcher/hook_thipx32.c launcher/hook_wsock32.c dll/thipx32.c dll/wsock32.c common/sockets.c common/config.c common/upnp.c resources/resource.o $(LIBS)

thipx32: dll/thipx32.c common/sockets.c common/config.c common/upnp.c
	sed 's/__REV__/$(REV)/g' resources/dll.rc.in | sed 's/__FILE__/thipx32/g' | sed 's/__GAME__/Command \& Conquer 95/g' | $(WINDRES) -O coff -o resources/dll.o
	$(CC) $(CFLAGS) -DBUILD_DLL -Wl,--enable-stdcall-fixup -shared -s -o thipx32.dll dll/thipx32.c common/sockets.c common/config.c common/upnp.c dll/thipx32.def resources/dll.o $(LIBS)

wsock32: dll/wsock32.c common/sockets.c common/config.c common/upnp.c
	sed 's/__REV__/$(REV)/g' resources/dll.rc.in | sed 's/__FILE__/wsock32/g' | sed 's/__GAME__/Red Alert 3.03+, Tiberian Sun and Red Alert 2/g' | $(WINDRES) -O coff -o resources/dll.o
	$(CC) $(CFLAGS) -DBUILD_DLL -Wl,--enable-stdcall-fixup -shared -s -o wsock32.dll dll/wsock32.c common/sockets.c common/config.c common/upnp.c dll/wsock32.def resources/dll.o $(LIBS)

cncnet-server: server/server.c common/sockets.c common/protocol.c
	gcc $(CFLAGS) -o cncnet-server server/server.c common/sockets.c common/protocol.c

upnp: ext/libminiupnpc.a
	$(CC) $(CFLAGS) -Iext/include -Lext -DUPNP_TEST -o upnp.exe common/upnp.c common/sockets.c $(LIBS)

clean:
	rm -f cncnet.exe launcher.exe cncnet-server resources/resource.o thipx32.dll wsock32.dll
